package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheUser;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodsDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SimpleReviewDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SimpleReviewDetailDO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.SimpleReviewInsertDTO;
import top.lixunda.ecommerce.server.good.api.entity.dto.SimpleReviewQueryDTO;
import top.lixunda.ecommerce.server.good.mapper.SimpleReviewMapper;
import top.lixunda.ecommerce.server.good.service.IGoodsService;
import top.lixunda.ecommerce.server.good.service.ISimpleReviewDetailService;
import top.lixunda.ecommerce.server.good.service.ISimpleReviewService;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 'commerce_goods.order_detail' is not BASE TABLE 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2020-01-06
 */
@Service
public class SimpleReviewServiceImpl
        extends BaseServiceImpl<SimpleReviewMapper, SimpleReviewDO>
        implements ISimpleReviewService {

    private final ISimpleReviewDetailService simpleReviewDetailService;

    private final IGoodsService goodsService;

    @Autowired
    public SimpleReviewServiceImpl(ISimpleReviewDetailService simpleReviewDetailService,
                                   IGoodsService goodsService) {
        this.simpleReviewDetailService = simpleReviewDetailService;
        this.goodsService = goodsService;
    }

    /**
     * 根据id查询单个评论信息
     *
     * @param id 评论id
     * @return 评论信息
     */
    @Override
    public SimpleReviewDetailDO queryReviewById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("评论id不能为空");
        }
        return simpleReviewDetailService.getById(String.valueOf(id));
    }

    /**
     * 根据多个id查询多个评论信息
     *
     * @param ids 多个评论id
     * @return 多个评论信息
     */
    @Override
    public List<SimpleReviewDetailDO> queryReviewsByIds(List<Integer> ids) {
        if (ObjectValidators.isEmpty(ids)) {
            throw getAppException("评论id不能为空");
        }
        if (ids.size() == 1) {
            SimpleReviewDetailDO simpleReviewDO = queryReviewById(ids.get(0));
            if (ObjectValidators.isEmpty(simpleReviewDO)) {
                return Collections.emptyList();
            }
            return Collections.singletonList(simpleReviewDO);
        }
        return simpleReviewDetailService.list(
                new LambdaQueryWrapper<SimpleReviewDetailDO>()
                        .in(SimpleReviewDetailDO::getId, ids));
    }

    /**
     * 查询某个商品的简单评论
     *
     * @param id 商品id
     * @return 评论信息
     */
    @Override
    public List<SimpleReviewDetailDO> queryReviewByGoodId(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("商品id不能为空");
        }
        return simpleReviewDetailService.list(
                new LambdaQueryWrapper<SimpleReviewDetailDO>()
                        .eq(SimpleReviewDetailDO::getGoodId, id));
    }

    /**
     * 分页查询某个商品的简单评论
     *
     * @param pageDTO 分页数据，包含条件
     * @return 分页数据
     */
    @Override
    public IPage<SimpleReviewDetailDO> queryReviewWrapperPage(PageDTO<SimpleReviewDetailDO, SimpleReviewQueryDTO> pageDTO) {
        if (ObjectValidators.isEmpty(pageDTO)) {
            throw getAppException("参数不能为空");
        }
        return simpleReviewDetailService.page(pageDTO.getPage(),
                pageDTO.getWrapper());
    }

    /**
     * 对某一个商品添加一条评论信息
     *
     * @param simpleReviewInsertDTO 商品评论信息
     * @return 添加成功的评论信息
     */
    @Override
    public SimpleReviewDO addReview(SimpleReviewInsertDTO simpleReviewInsertDTO) {
        CacheUser userInfoDO = currentUserCacheNotNull();
        List<GoodsDO> goodsDoList = goodsService.queryByIds(Collections.singletonList(simpleReviewInsertDTO.getId()));
        if (ObjectValidators.isEmpty(goodsDoList)) {
            throw getAppException("商品不存在");
        }
        GoodsDO goodsDO = goodsDoList.get(0);
        SimpleReviewDO simpleReviewDO = new SimpleReviewDO();
        simpleReviewDO.setGoodId(goodsDO.getId());
        simpleReviewDO.setGoodName(goodsDO.getName());
        simpleReviewDO.setStoreId(goodsDO.getStoreId());
        simpleReviewDO.setStoreName(goodsDO.getStoreName());
        simpleReviewDO.setScore(simpleReviewInsertDTO.getScore());
        simpleReviewDO.setContent(simpleReviewInsertDTO.getContent());
        simpleReviewDO.setUserId(userInfoDO.getAccount());
        simpleReviewDO.setUserName(userInfoDO.getName());
        simpleReviewDO.setTime(LocalDateTime.now());
        boolean success = this.save(simpleReviewDO);
        if (!success) {
            throw getAppException("保存数据失败");
        }
        return simpleReviewDO;
    }

}
